package cn.trda.controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.trda.util.ValidationCodeUtil;

@Controller
@RequestMapping("/captchca")
public class CaptchcaController {
	
	@RequestMapping("/get")
	public void get(HttpServletResponse response,HttpSession session) throws Exception{
		String captchca = ValidationCodeUtil.getCode(response);
		//保存到session，后面验证需要用到
		session.setAttribute("captchca", captchca);
	}
	
	//验证验证码是否正确
	@RequestMapping(value="/validate",method=RequestMethod.POST)
	public @ResponseBody String validate(String captchca,HttpSession session) throws Exception{
		if(captchca.equalsIgnoreCase((String)session.getAttribute("captchca"))){
			return "success";
		}else{
			return "fail";
		}
	}
}
